package com.lw.edu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lw.commonutil.result.ResultCodeEnum;
import com.lw.edu.client.VodClient;
import com.lw.edu.entity.Chapter;
import com.lw.edu.entity.Course;
import com.lw.edu.entity.CourseDescription;
import com.lw.edu.entity.Video;
import com.lw.edu.entity.frontvo.CourseQueryVo;
import com.lw.edu.entity.frontvo.CourseWebVo;
import com.lw.edu.entity.vo.CourseInfoVo;
import com.lw.edu.entity.vo.CoursePublishVo;
import com.lw.edu.entity.vo.CourseQuery;
import com.lw.edu.mapper.CourseMapper;
import com.lw.edu.service.ChapterService;
import com.lw.edu.service.CourseDescriptionService;
import com.lw.edu.service.CourseService;
import com.lw.edu.service.VideoService;
import com.lw.servicebase.exception.GuliException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;


@Service
@Transactional
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements CourseService {
    @Autowired
    private CourseDescriptionService courseDescriptionService;
    @Autowired
    private ChapterService chapterService;
    @Autowired
    private VideoService videoService;
    @Autowired
    private VodClient vodClient;
    @Override
    public String addCourseInfo(CourseInfoVo courseInfoVo) {
        //向课程表添加课程基本信息
        Course course = new Course();
        BeanUtils.copyProperties( courseInfoVo,course );
        int insert = baseMapper.insert( course );
        if (insert==0){
            //添加失败
            throw new GuliException( ResultCodeEnum.FAIL );
        }
        //向课程简介表添加课程简介
        CourseDescription courseDescription = new CourseDescription();
        BeanUtils.copyProperties( courseInfoVo,courseDescription );
        courseDescription.setId( course.getId() ); //共享主键
        courseDescriptionService.save( courseDescription );
        return course.getId();
    }

    //根据课程id查找
    @Override
    public CourseInfoVo getCourseInfo(String courseId) {
        //查询课程表
        Course course = baseMapper.selectById( courseId );
        CourseInfoVo courseInfoVo = new CourseInfoVo();
        BeanUtils.copyProperties( course,courseInfoVo );
        //查询课程描述表
        CourseDescription courseDescription = courseDescriptionService.getById( courseId );
        //courseInfoVo.setDescription( courseDescription.getDescription() );
        BeanUtils.copyProperties( courseDescription,courseInfoVo );
        return courseInfoVo;
    }

    //修改课程对象
    @Override
    public void updateCourseInfo(CourseInfoVo courseInfoVo) {
        //修改课程表
        Course course = new Course();
        BeanUtils.copyProperties( courseInfoVo,course );
        baseMapper.updateById( course );
        //修改描述表
        CourseDescription courseDescription = new CourseDescription();
        BeanUtils.copyProperties( courseInfoVo,courseDescription );
        courseDescriptionService.updateById( courseDescription );
    }

    @Override
    public CoursePublishVo getCoursePublish(String courseId) {
        CoursePublishVo coursePublishVo = baseMapper.getCoursePublishInfo( courseId );
        return coursePublishVo;
    }

    @Override
    public boolean deleteCourse(String courseId) {
        //删除课程 需要删除 课程和课程描述 章节和小节
        //删除小节 需要删除对应视频

        //课程章节 一对多
        //章节小节 一对多
        int i = baseMapper.deleteById( courseId );
        if (i==0){
            return false;
        }
        boolean b = courseDescriptionService.removeById( courseId );
        if (!b){
            return false;
        }
        QueryWrapper<Chapter> chapterWrapper=new QueryWrapper<>();
        chapterWrapper.eq( "course_id",courseId );
        //先查后删
        List<Chapter> chapterList = chapterService.list( chapterWrapper );
        if (!chapterList.isEmpty()){
            boolean chapterFlag = chapterService.remove( chapterWrapper );
            if (!chapterFlag){
                return false;
            }
        }
        QueryWrapper<Video> videoWrapper=new QueryWrapper<>();
        //先查后删
        videoWrapper.eq( "course_id",courseId );
        List<Video> videoList = videoService.list( videoWrapper );
        if (!videoList.isEmpty()){
            List<String> ids=new ArrayList<>();
            videoList.stream().forEach( video -> {
                String videoSourceId = video.getVideoSourceId();
                if (videoSourceId!=null){
                    ids.add( videoSourceId );
                }
            } );
            //批量删除阿里云视频
            if (!ids.isEmpty()){
                vodClient.removeBatch( ids );
            }
            boolean videoFlag = videoService.remove( videoWrapper );
            if (!videoFlag){
                return false;
            }
        }
        return true;
    }

    //条件带分页查询
    @Override
    public Page<Course> pageList(Page page, CourseQuery courseQuery) {
        QueryWrapper<Course> wrapper=new QueryWrapper<>();
        if (!StringUtils.isEmpty( courseQuery.getTitle() )){
            wrapper.like( "title",courseQuery.getTitle() );
        }
        if (!StringUtils.isEmpty( courseQuery.getStatus() )){
            wrapper.eq( "status",courseQuery.getStatus() );
        }
        Page pageList = baseMapper.selectPage( page, wrapper );
        return pageList;
    }

    //条件带分页查询（前台）
    @Override
    public Page<Course> getCourseList(Page<Course> coursePage, CourseQueryVo courseQuery) {
        QueryWrapper<Course> queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(courseQuery.getSubjectParentId())) {
            queryWrapper.eq("subject_parent_id", courseQuery.getSubjectParentId());
        }

        if (!StringUtils.isEmpty(courseQuery.getSubjectId())) {
            queryWrapper.eq("subject_id", courseQuery.getSubjectId());
        }

        if (!StringUtils.isEmpty(courseQuery.getBuyCountSort())) {
            queryWrapper.orderByDesc("buy_count");
        }

        if (!StringUtils.isEmpty(courseQuery.getGmtCreateSort())) {
            queryWrapper.orderByDesc("gmt_create");
        }

        if (!StringUtils.isEmpty(courseQuery.getPriceSort())) {
            queryWrapper.orderByDesc("price");
        }
        Page<Course> pageList = baseMapper.selectPage( coursePage, queryWrapper );
        return pageList;
    }

    @Override
    public CourseWebVo getBaseCourseInfo(String id) {
        return baseMapper.getBaseCourseInfo(id);
    }
}
